home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8524 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.1 KB  |  140 lines

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: separate compilation
  5. Date: 02 Mar 1996 20:06:22 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Mar2130622@qcd.lanl.gov>
  8. References: <1996Mar2.164559.24931@dcs.warwick.ac.uk>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: D.C.Molero@dcs.warwick.ac.uk's message of Sat, 2 Mar 1996 16:45:59 GMT
  13.  
  14. In article <1996Mar2.164559.24931@dcs.warwick.ac.uk>
  15. D.C.Molero@dcs.warwick.ac.uk (Daniel Castillo Molero) writes:
  16.  
  17. <snip>
  18. DCM: /* declare the structure side as extern to avoid having to redeclare it,
  19. DCM:    since it is already declared in file2.c
  20. DCM: */
  21. DCM: extern struct side;
  22.  
  23. This makes no sense. extern helps specify the linkage of identifiers,
  24. struct tags have no linkage. In other words, just specifying `extern
  25. struct side;' does not mean that the compiler will search through all
  26. your files to find what struct side is.
  27.  
  28. The contents of a struct need to be known by the compiler in some
  29. cases. Use `extern' for variables and function names if you want, not
  30. for struct tags.
  31.  
  32. The common way to solve the problem is to have a small file `side.h'
  33. which contains the declaration
  34.  
  35. struct side {
  36.    int a;
  37.    struct side *next;
  38. }
  39.  
  40. and then say #include "side.h" in whichever file needs it.
  41.  
  42.  
  43. DCM: 
  44. DCM: int main(void) {
  45. DCM: struct side *tmp;
  46. DCM: /* first_side is declared in file2.c and points to the first element
  47. DCM:    of the list.
  48. DCM: */
  49. DCM: extern struct side *first_side;
  50.  
  51. It is best to put this line in a .h file as well. Then you can include
  52. the same .h file both here and in file2.c, and the compiler will check
  53. that the declaration matches the definition.
  54.  
  55. Moreover, many people consider that extern declarations should not
  56. appear inside blocks: there are cases where they are useful. In this
  57. particular case, I would rather declare it ouside all functions.
  58.  
  59. DCM: /* print the three elements */
  60. DCM: 
  61. DCM:     for (tmp = first_side; tmp; tmp = tmp->next)
  62. DCM:         printf("side: %d\n", tmp->a);
  63.  
  64. To use printf you have to #include <stdio.h> at the top of your file.
  65.  
  66. So you never actually call any of the functions in the other files?
  67. Remember all that your program done is evaluate main() afetr global
  68. initializations: if mai calls nothing else, nothing else is done.
  69.  
  70. DCM: 
  71. DCM:     return 0;
  72. DCM: }
  73. DCM: 
  74. DCM: 
  75. DCM: file2.c
  76. DCM: -------
  77. DCM: 
  78. DCM: /* type of each element of the linked list */
  79. DCM: struct side {
  80. DCM:     int a;
  81. DCM:     struct side *next;
  82. DCM: };
  83. DCM: 
  84. DCM: void store_side(struct side* i, struct side** first_side);
  85.  
  86. It is best to put this line in a .h file as well. Then you can include
  87. the same .h file both here and in file3.c, and the compiler will check
  88. that the declaration matches the definition.
  89.  
  90. DCM: 
  91. DCM: void create_list(void)
  92. DCM: {
  93. DCM: struct side *i;
  94. DCM: struct side *first_side;
  95.  
  96. Hmmm. You expect all your local variables in every file to be visible
  97. in every other file? Writing programs would become impossible then:
  98. one would have to remember all the names used everywhere to avoid all
  99. conflicts. 
  100.  
  101. If you want `first_side' to be visible to other programs, give it
  102. external linkage. To do that, define it outside all functions.
  103.  
  104. <snip>
  105. DCM:     i = malloc(1*sizeof(struct side));
  106.  
  107. To use malloc, you must #include <stdlib.h> at the top of your file. 
  108.  
  109. <snip>
  110. DCM: }
  111. DCM: 
  112. DCM: 
  113. DCM: file3.c
  114. DCM: -------
  115. DCM: 
  116. DCM: /* declare the structure side as extern, as in file1.c */
  117. DCM: extern struct side; 
  118.  
  119. Same problem as in file1.c
  120.  
  121. DCM: 
  122. DCM: /* insert a new element in the list */
  123. DCM: void
  124. DCM: store_side(struct side* i, struct side** first_side)
  125. DCM: {
  126. DCM:     i->next = *first_side;
  127. DCM:     *first_side = i;
  128. DCM: }
  129. DCM: 
  130.  
  131. Cheers
  132. Tanmoy
  133. --
  134. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  135. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  136. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  137. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  138. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  139. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  140.